﻿using System;
using System.Net.Mail;

namespace Fly.Common.Email
{
    /// <summary>
    /// 简单邮件发送.
    /// </summary>
    public class EmailService : IEmailService
    {
        private EmailServiceSettings _config;

        /// <summary>
        /// 初始化 <see cref="EmailService"/> 类.
        /// </summary>
        public EmailService()
        {
        }

        /// <summary>
        /// 初始化 <see cref="EmailService"/> 类.
        /// </summary>
        /// <param name="settings">邮件配置类.</param>
        public EmailService(EmailServiceSettings settings)
        {
            Init(settings);
        }


        /// <summary>
        /// 初始化配置信息.
        /// </summary>
        /// <param name="config"></param>
        public void Init(EmailServiceSettings config)
        {
            _config = config;
        }


        #region IEmailService Members
        /// <summary>
        /// 获取或设置邮件服务的配置信息.
        /// </summary>
        public EmailServiceSettings Settings
        {
            get { return _config; }
            set { _config = value; }
        }


        /// <summary>
        /// 发送一封邮件.
        /// </summary>
        /// <param name="message">邮件信息.</param>
        /// <param name="credentialsUser">邮件服务器用户名.</param>
        /// <param name="credentialsPassword">邮件服务器密码.</param>
        /// <returns></returns>
        public bool Send(NotificationMessage message, string credentialsUser, string credentialsPassword)
        {
            MailMessage mailMessage = new MailMessage(_config.From, message.To, message.Subject, message.Body)
            {
                IsBodyHtml = message.IsHtml
            };
            return InternalSend(mailMessage, true, credentialsUser, credentialsPassword);
        }


        /// <summary>
        /// 发送一封邮件.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool Send(NotificationMessage message)
        {
            MailMessage mailMessage = new MailMessage(_config.From, message.To, message.Subject, message.Body)
            {
                IsBodyHtml = message.IsHtml
            };
            return InternalSend(mailMessage, Settings.IsAuthenticationRequired, Settings.AuthenticationUserName, Settings.AuthenticationPassword);
        }


        /// <summary>
        /// 发送一封邮件.
        /// </summary>
        /// <param name="message">邮件内容</param>
        /// <param name="useCredentials">邮件服务器是否需要进行安全认证</param>
        /// <param name="credentialsUser">安全认证所需的用户名.</param>
        /// <param name="credentialsPassword">安全认证所需的密码.</param>
        /// <returns></returns>
        public bool Send(MailMessage message, bool useCredentials, string credentialsUser, string credentialsPassword)
        {
            return InternalSend(message, useCredentials, credentialsUser, credentialsPassword);
        }


        /// <summary>
        /// 使用默认配置发送一封邮件.
        /// </summary>
        /// <returns></returns>
        public bool Send(MailMessage mailMessage)
        {
            return InternalSend(mailMessage, Settings.IsAuthenticationRequired, Settings.AuthenticationUserName, Settings.AuthenticationPassword);
        }
        #endregion



        /// <summary>
        /// 初始化发送信息.
        /// </summary>
        /// <param name="mailMessage">The message.</param>
        /// <param name="useCredentials">if set to <c>true</c> [use credentials].</param>
        /// <param name="credentialsUser">The credentials user.</param>
        /// <param name="credentialsPassword">The credentials password.</param>
        /// <returns></returns>
        private bool InternalSend(MailMessage mailMessage, bool useCredentials, string credentialsUser, string credentialsPassword)
        {
            bool sent = true;
            try
            {
                string host = _config.SmptServer;
                int port = _config.Port;

                SmtpClient client = _config.UsePort ? new SmtpClient(host, port) : new SmtpClient(host);

                if (useCredentials)
                {
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential(credentialsUser, credentialsPassword);
                }
                client.Send(mailMessage);
            }
            catch (Exception)
            {
               // Logger.Error("Unable to send email.", ex);
                sent = false;
            }
            return sent;
        }

    }
}
